﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using MSH.Common.Classes;
using System.Windows.Media.Imaging;
using System.IO;
using MSH.Common.DAO;
using WP7Client.Helpers;
using WP7Client.Controller;
using System.Collections.ObjectModel;
using System.Collections;

namespace WP7Client.Classes
{
    public class BooleanToInvertedBooleanConverter : IValueConverter
    {
        /// <summary>
        /// Inverteert een boolean
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return false;

            if (value is Boolean)
                return !(Boolean)value;

            return false;          
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return true;
        }
    }

    public class NullToBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value != null);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return true;
        }
    }

    public class DirectionToMarginConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;

            if (value is Boolean)
            {
                if ((Boolean)value == true)
                    return new Thickness(5,5,60,5);
                else
                    return new Thickness(60, 5, 5, 5);
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return true;
        }
    }

    public class DirectionToHorizontalAlignmentConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;

            if (value is Boolean)
            {
                if ((Boolean)value == true)
                    return HorizontalAlignment.Left;
                else
                    return HorizontalAlignment.Right;
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return true;
        }
    }

    public class ObjectToVisibilityConverter : IValueConverter
    {
        /// <summary>
        /// Returned op basis van een object een System.Visibility
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            String param = "";
            if (parameter != null && parameter.GetType() == typeof(String))
                param = (String)parameter;

            if (value == null)
            {
                if (param == "invert")
                    return Visibility.Visible;
                else
                    return Visibility.Collapsed;
            }
            else
            {
                if (param == "invert")
                    return Visibility.Collapsed;
                else
                    return Visibility.Visible;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class BoolToVisibilityConverter : IValueConverter
    {
        /// <summary>
        /// Converteert een boolean naar een System.Visibility
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return Visibility.Collapsed;
            else if (value.GetType() == typeof(Boolean))
            {
                String invert = null;
                if (parameter != null && parameter.GetType() == typeof(String))
                    invert = (String)parameter;

                if ((Boolean)value == true)
                {
                    if (invert == "invert")
                        return Visibility.Collapsed;
                    else
                        return Visibility.Visible;
                }
                else
                {
                    if (invert == "invert")
                        return Visibility.Visible;
                    else
                        return Visibility.Collapsed;
                }
            }
            else
                return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class DateTimeFormatConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.GetType() == typeof(DateTime))
            {
                String result = ((DateTime)value).ToString("dd-MM-yyyy HH:mm");
                return result;
            }
            else
                return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class FriendImageConverter : IValueConverter
    {
        /// <summary>
        /// Converteer de blokkeringsstatus naar een passende afbeelding
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.GetType() == typeof(bool))
            {
                if ((bool)value)
                    return new ImageSourceConverter().ConvertFromString("/WP7Client;component/Resources/Images/Blocked_user_icon.png");
                else
                    return new ImageSourceConverter().ConvertFromString("/WP7Client;component/Resources/Images/avatar.png");
            }
            else
                return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class PrivacyEnumConverter : IValueConverter
    {
        /// <summary>
        /// Converteert een privacy enumeratie naar een index
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.GetType() == typeof(PrivacyEnum))
            {
                PrivacyEnum privacy = (PrivacyEnum)value;
                switch (privacy)
                {
                    case PrivacyEnum.Friends:
                        return 0;
                    case PrivacyEnum.Private:
                        return 2;
                    case PrivacyEnum.Public:
                        return 1;
                    default:
                        return 1;
                }
            }
            else
                return 1;
        }

        /// <summary>
        /// Converteert een index naar een PrivacyEnum
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.GetType() == typeof(Int32))
            {
                Int32 index = System.Convert.ToInt32(value);
                switch (index)
                {
                    case 0:
                        return PrivacyEnum.Friends;
                    case 2:
                        return PrivacyEnum.Private;
                    case 1:
                        return PrivacyEnum.Public;
                    default:
                        return PrivacyEnum.Public;
                }
            }
            else
                return 1;
        }


       

    }

    public class ByteArrayToImageSource : IValueConverter
    {
        /// <summary>
        /// Converteert een BitmapImage naar een byte array
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.GetType() == typeof(Byte[]))
            {
                BitmapImage img = new BitmapImage();
                using (MemoryStream stream = new MemoryStream(value as byte[]))
                {
                    try
                    {
                        img.SetSource(stream);
                    }
                    catch (Exception ex) { }
                }

                return img;
            }
            else
                return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class GenderEnumConverter : IValueConverter
    {
        /// <summary>
        /// Converteert de Gender enumeratie naar een index
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.GetType() == typeof(GenderEnum))
            {
                GenderEnum privacy = (GenderEnum)value;
                switch (privacy)
                {
                    case GenderEnum.Male:
                        return 0;
                    case GenderEnum.Female:
                        return 1;
                    case GenderEnum.Unknown:
                        return 2;
                    default:
                        return 0;
                }
            }
            else
                return 1;
        }

        /// <summary>
        /// Converteert een index naar een GenderEnum
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.GetType() == typeof(Int32))
            {
                Int32 index = System.Convert.ToInt32(value);
                switch (index)
                {
                    case 0:
                        return GenderEnum.Male;
                    case 1:
                        return GenderEnum.Female;
                    case 2:
                        return GenderEnum.Unknown;
                    default:
                        return GenderEnum.Male;
                }
            }
            else
                return 1;
        }
    }

    public class ObjectWithPrivacyToVisibilityConverter : IValueConverter
    {
        /// <summary>
        /// Vertaalt een PrivacyEnum naar een System.Visibility
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            String param = "";
            if (parameter != null && parameter.GetType() == typeof(String))
                param = (String)parameter;

            if (!(value is SearchResult) && !(value is PersonNearby) && !(value is Friend))
                return null;
            if (value is SearchResult)
            {
                SearchResult result = value as SearchResult;

                switch (result.ProfilePrivacy)
                {
                    case PrivacyEnum.Private:
                        return Visibility.Collapsed;
                    case PrivacyEnum.Friends:
                        if (result.IsFriend)
                            return Visibility.Visible;
                        else
                            return Visibility.Collapsed;
                    case PrivacyEnum.Public:
                        return Visibility.Visible;
                }
            }
            else if (value is PersonNearby)
            {
                PersonNearby result = value as PersonNearby;

                switch (result.profilePrivacy)
                {
                    case PrivacyEnum.Private:
                        return Visibility.Collapsed;
                    case PrivacyEnum.Friends:
                        if (result.isFriend)
                            return Visibility.Visible;
                        else
                            return Visibility.Collapsed;
                    case PrivacyEnum.Public:
                        return Visibility.Visible;
                }
            }
            else if (value is Friend)
            {
                //suggesties
                Friend result = value as Friend;
                switch (result.profilePrivacy)
                {
                    case PrivacyEnum.Private:
                        return Visibility.Collapsed;
                    case PrivacyEnum.Friends:
                        return Visibility.Collapsed;
                    case PrivacyEnum.Public:
                        return Visibility.Visible;
                }
            }

            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class ObjectWithPrivacyToProfileCommandByPeopleNearbyConverter : IValueConverter
    {
        /// <summary>
        /// Vertaalt een PrivacyEnum naar een ICommand
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            String param = "";
            if (parameter != null && parameter.GetType() == typeof(String))
                param = (String)parameter;

            if (!(value is PersonNearby) && !(value is SearchResult))
                return null;

            if (value is SearchResult)
            {
                SearchResult result = value as SearchResult;

                switch (result.ProfilePrivacy)
                {
                    case PrivacyEnum.Private:
                        return null;
                    case PrivacyEnum.Friends:
                        if (result.IsFriend)
                            return (ControllerHelper.OverviewController.OverviewPage.PivotItemPeopleNearby.DataContext as PeopleNearbyController).ShowProfileCommand;
                        else
                            return null;
                    case PrivacyEnum.Public:
                        return (ControllerHelper.OverviewController.OverviewPage.PivotItemPeopleNearby.DataContext as PeopleNearbyController).ShowProfileCommand;
                }
            }
            else if (value is PersonNearby)
            {
                PersonNearby result = value as PersonNearby;

                switch (result.profilePrivacy)
                {
                    case PrivacyEnum.Private:
                        return null;
                    case PrivacyEnum.Friends:
                        if (result.isFriend)
                            return (ControllerHelper.OverviewController.OverviewPage.PivotItemPeopleNearby.DataContext as PeopleNearbyController).ShowProfileCommand;
                        else
                            return null;
                    case PrivacyEnum.Public:
                        return (ControllerHelper.OverviewController.OverviewPage.PivotItemPeopleNearby.DataContext as PeopleNearbyController).ShowProfileCommand;
                }
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
    
    public class ObjectWithPrivacyToProfileCommandBySuggestionsConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            String param = "";
            if (parameter != null && parameter.GetType() == typeof(String))
                param = (String)parameter;

            if (!(value is Friend))
                return null;

            if (value is Friend)
            {
                Friend result = value as Friend;

                switch (result.profilePrivacy)
                {
                    case PrivacyEnum.Private:
                        return null;
                    case PrivacyEnum.Friends:
                        return null;
                    case PrivacyEnum.Public:
                        return (ControllerHelper.OverviewController.OverviewPage.PivotItemSuggestions.DataContext as SuggestionController).ShowProfileCommand;
                }
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
    
    public class ObjectWithPrivacyToProfileCommandByInvitationsConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            String param = "";
            if (parameter != null && parameter.GetType() == typeof(String))
                param = (String)parameter;

            if (value is Friend)
            {
                Friend result = value as Friend;

                switch (result.profilePrivacy)
                {
                    case PrivacyEnum.Private:
                        return null;
                    case PrivacyEnum.Friends:
                        return null;
                    case PrivacyEnum.Public:
                        return (ControllerHelper.OverviewController.OverviewPage.PivotItemInvitations.DataContext as InvitationController).ShowProfileCommand;
                }
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class ObservableCollectionCountToVisibility : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                if ((value as ICollection).Count > 0)
                    return Visibility.Visible;
                else
                    return Visibility.Collapsed;
            }
            else
                return Visibility.Collapsed;
            
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class UserLocationConverter : IValueConverter
    {
        /// <summary>
        /// Converteert de locationdata naar een string
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value is UserLocationData)
            {
                String result = "";
                UserLocationData location = value as UserLocationData;

                if (!String.IsNullOrEmpty(location.AddressLine))
                    result += (location.AddressLine + ", ");
                if (!String.IsNullOrEmpty(location.Locality))
                    result += (location.Locality + ", ");
                if (!String.IsNullOrEmpty(location.AdminDistrict))
                    result += (location.AdminDistrict + ", ");
                if (!String.IsNullOrEmpty(location.CountryRegion))
                    result += location.CountryRegion;
                if (result.EndsWith(", "))
                    result.Remove((result.Length - 2));

                if(!String.IsNullOrEmpty(result))
                    result += String.Format(" {0}", DateTime.Now.ToString("HH:mm"));

                return result;
            }
            else
                return "";

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class NullToVisibilityConverter : IValueConverter
    {
        /// <summary>
        /// Returned een System.Visibility waarde aan de hand van de value waarde
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == null ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class MapVisibilityEnumConverter : IValueConverter
    {
        /// <summary>
        /// Converteert de MapVisibility naar een index
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.GetType() == typeof(MapVisibility))
            {
                MapVisibility mapVisibility = (MapVisibility)value;
                switch (mapVisibility)
                {
                    case MapVisibility.FriendsAndStrangers:
                        return 0;
                    case MapVisibility.FriendsOnly:
                        return 1;
                    case MapVisibility.StrangersOnly:
                        return 2;
                    default:
                        return 0;
                }
            }
            else
                return 0;  
        }

        /// <summary>
        /// Converteert een index naar een MapVisibility
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.GetType() == typeof(Int32))
            {
                Int32 index = System.Convert.ToInt32(value);
                switch (index)
                {
                    case 0:
                        return MapVisibility.FriendsAndStrangers;
                    case 1:
                        return MapVisibility.FriendsOnly;
                    case 2:
                        return MapVisibility.StrangersOnly;
                    default:
                        return MapVisibility.FriendsAndStrangers;
                }
            }
            else
                return 1;
        }
    }

    public class IsFriendConverter : IValueConverter
    {
        /// <summary>
        /// Controleer of twee gebruikers vrienden van elkaar zijn
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Boolean invert = parameter != null && parameter is String && parameter.ToString().Equals("invert");

            if (value != null && value is String)
            {
                lock (ControllerHelper.FriendController.Friends)
                {
                    foreach (Friend friend in ControllerHelper.FriendController.Friends)
                    {
                        if (friend.Username.Equals(value.ToString()))
                            return invert ? Visibility.Collapsed : Visibility.Visible;
                    }
                }
            }
            return invert ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class CanChatConverter : IValueConverter
    {
        /// <summary>
        /// Controleert of twee gebruikers kunnen chatten
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Boolean invert = parameter != null && parameter is String && parameter.ToString().Equals("invert");

            if (value != null && value is String)
            {
                lock (ControllerHelper.FriendController.Friends)
                {
                    foreach (Friend friend in ControllerHelper.FriendController.Friends)
                    {
                        if (friend.Username.Equals(value.ToString()))
                            return invert ? Visibility.Collapsed : Visibility.Visible;
                    }
                }

                lock (ControllerHelper.FriendController.Friends)
                {
                    foreach (PersonNearby person in ControllerHelper.PeopleNearbyController.PeopleNearby)
                    {
                        if (person.Username.Equals(value.ToString()))
                            return invert ? Visibility.Collapsed : Visibility.Visible;
                    }
                }
            }
            return invert ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

   
}
